package com.htrq.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.htrq.entity.CNGData;
import com.htrq.entity.Page;
import com.htrq.entity.User;
import com.htrq.service.CNGService;

@Controller
@RequestMapping("jsp/owork")
public class CNGDataController {
	@Resource
	private CNGService cngs;

	@RequestMapping("getAllCNGdata")
	@ResponseBody
	public String getAllCNGdata(CNGData cng, Page page, HttpServletResponse response) throws IOException {
		List<CNGData> list = cngs.getAllCNGdata(cng, page);
		String str = JSON.toJSONString(list);
		Long total = cngs.getTotal(cng);
		String json = "{total:" + total + ",cngs:" + str + "}";
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		out.write(json);
		return null;
	}
	@RequestMapping("saveCNGData")
	@ResponseBody
	public String saveCNGData(CNGData cng, HttpServletRequest request, HttpServletResponse response) throws IOException {
		User user = (User) request.getSession().getAttribute("user");
		Date date = new Date();
		cng.setRemark(cng.getRemark() + user.getRelname() + "于"
				+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date).toString() + "修改");
		boolean flag = cngs.modify(cng);
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		if (flag) {
			out.print("{'success':true,'res':{'result':'修改成功,请点刷新'}}");
		} else {
			out.print("{'success':true,'res':{'result':'修改失败'}}");
		}
		return null;
	}
}
